#pragma once
#include "Panel.h"
#include "ContainerAndImagePanels.h"
#include "JpegDecoder.h"
class ParserDICOM : public QObject
{
	Q_OBJECT
public:	
signals:
	void signalLoadedFile(unsigned int current, unsigned int total);
public:	
	~ParserDICOM(void);
	DICOMFile * parse(const QString &filePath, const QString & params = QString(""), const QString & filter = QString(""));
	unsigned int pos;		
	unsigned int size;
	unsigned int currentByte;
	ParserDICOM(void);		
private:
	QMap <QString, QString> correspondenceCodecs;
	DICOMFile * parseFile(const QString &filePath, const QString & params = QString(""), const QString & filter = QString(""));
//	DICOMFile * parseFile(const QString &filePath, const QString & params, const QString & filter);
	DICOMFile * parseMemory(char * pointer, const QString & params = QString(""), const QString & filter= QString(""), int size = 0);
	DICOMFile * initAndParsing( const QString & params, const QString & filter);	
	BOOLEAN loadFromMem;
	void * memPointer;		
private:	
	//QString typeElement;		
	QFile file;
	TElementParser * parser;
	BOOLEAN isFirstTag;
	int parentGroupTagSeq;
	int parentItemTagSeq;
	int parentGroupTagSeqPrev;
	int parentItemTagSeqPrev;
	HANDLE DICOMFileHandle;
	QString filePath;
	int curDirectoryRecordType;
	BOOLEAN isCreated;
	void parseVDRExternData();	
	BOOLEAN parseVDRFile (const QString & fileName);
	//BOOLEAN parseVDRFile (const QString & fileName);
	inline void doDicomInformatoin(int, int, char *, unsigned int);
	int TranferSyntax;		
	void * pImageDICOM;
	void * pImageApplication;
	//DICOMStruct  * dicomStruct;
	DICOMFile * dicomFile;
	FileHeader * fileHeader;	
	char * allTypes;			
	char * errorInformation;
	char * firstTypes;
	static QStringList typeOneElementTagTypes;
	static QStringList allElementTagTypes;
	//BOOLEAN loadFile(const char *);
	BOOLEAN loadFile(const QString & filePath);
	BOOLEAN parseFileMetaInformation();
	void parseFileMetaInformationExplicitVRBigEndian();
	void parseFileMetaInformationExplicitVRLittleEndian();
	void parseFileMetaInformationImplicitVRBigEndian();
	void parseFileMetaInformationImplicitVRLittleEndian();
	void checkPreamble();
	void checkEndianAndEcplicity();
	BOOLEAN parseBodyExplicitVRBigEndian();
	BOOLEAN parseBodyExplicitVRLittleEndian();
	BOOLEAN parseBodyImplicitVRBigEndian();
	BOOLEAN parseBodyImplicitVRLittleEndian();
	int parseBodyExplicitVRLittleEndianItemTag(unsigned int);
	int parseBodyExplicitVRLittleEndianReadSQ(unsigned int);
	int parseBodyExplicitVRLittleEndianReadOB(unsigned int);
	int parseBodyImplicitVRLittleEndianItemTag(unsigned int);
	int parseBodyImplicitVRLittleEndianReadSQ(unsigned int);		
	int deepParsing;
	JpegDecoder * jpegDecoder;
//	void decompressJPEG(void * valueOfItem, int length);
	void decompressJPEG(void * valueOfItem, int length, void * outputItem);
	//void decompressRLELossless(void * valueOfItem, int length);	
	void decompressRLELossless(void * valueOfItem, int length,void * outputItem );
	DWORD WINAPI SetFilePointer(__in HANDLE hFile, __in LONG lDistanceToMove, __inout_opt PLONG lpDistanceToMoveHigh, __in DWORD dwMoveMethod);
	BOOL WINAPI ReadFile(
		__in        HANDLE hFile,
		LPVOID lpBuffer,
		__in        DWORD nNumberOfBytesToRead,
		__out_opt   LPDWORD lpNumberOfBytesRead,
		__inout_opt LPOVERLAPPED lpOverlapped
		);
	BOOL WINAPI	CloseHandle(
		__in HANDLE hObject
		);
	
};






